package com.dwork.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.dwork.base.controller.utils.JsonUtil;
import com.dwork.base.controller.utils.RequestResponseContext;



public class BaseController {
	@Value("#{configProperties['themesName']}")
	private String themesName;
	
	public static final String STATUS = "status";
	public static final String STATUS_SUCCESS = "success";
	public static final String STATUS_FAILURE = "failure";
	public static final String MESSAGE = "message";
	public static final String SUCCESS_MESSAGE = "操作成功！";
	public static final String VALIDATE_ERROR_MESSAGE = "系统验证错误！";
	public static final String COMMON_ERROR_MESSAGE = "系统错误，请联系管理员！";
	public BaseController() {

	}
	
	public void putInToJsonResponse(Object content) {
		try {
			this.getResponse().setContentType("application/json;charset=UTF-8");
			this.getResponse().setCharacterEncoding("UTF-8");
			this.getResponse().getWriter().print(JsonUtil.toJson(content));
			this.getResponse().getWriter().flush();
			this.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private Validator validator = null;

	private Validator getValidator() {
		if(validator==null) {
			Object o = RequestContextUtils.getWebApplicationContext(getRequest()).getBean("validator");
			if(o!=null) {
				validator = (Validator) o;
			}
		}
		return validator;
	}
	
	public List<Map<String, String>> getValidatorMessage(Object o, Class<?>... groups) {
		List<Map<String, String>> messageList = new ArrayList<Map<String, String>>();
		Set<ConstraintViolation<Object>> set = getValidator().validate(o, groups); 
		for (ConstraintViolation<Object> constraintViolation : set) { 
			Map<String, String> map = new HashMap<String, String>();
			map.put("name", constraintViolation.getPropertyPath()+"");
			map.put("value", constraintViolation.getMessage());
			messageList.add(map);
		}
		return messageList;
	}
	
	
	
	
	
	public void putInToHtmlResponse(String content) {
		try {
			this.getResponse().setContentType("text/html;charset=UTF-8");
			this.getResponse().setCharacterEncoding("UTF-8");
			this.getResponse().getWriter().print(content);
			this.getResponse().getWriter().flush();
			this.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public HttpServletRequest getRequest() {
		return RequestResponseContext.getRequest();
	}


	public HttpServletResponse getResponse() {
		return RequestResponseContext.getResponse();
	}

	
	/**
	 * 
	 * @param viewName 从具体的主题文件夹下开始找，以“/”开头
	 * @return
	 */
	public ModelAndView getBaseMV(String viewName) {
		//viewName = "/ftl/head"
		ModelAndView mv = new ModelAndView(themesName+viewName);
		mv.addObject("contextPath", getRequest().getContextPath());
		mv.addObject("themesName", themesName);
		return mv;
	}
	
	public ModelAndView getFrameMV(String viewName) {
		//viewName = "/ftl/head"
		ModelAndView mv = new ModelAndView("default/ftl/frame");
		mv.addObject("target", viewName);
		return mv;
	}
	
	/**
	 * 指向拒绝访问页面
	 * @return
	 */
	public ModelAndView getRefuseMV() {
		ModelAndView mv = getBaseMV("/ftl/refuse");
		mv.addObject("message", "");
		return mv;
	}
	
	

}
